package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clqteam.model.Employee;
import com.clqteam.service.updataShService;
import com.clqteam.serviceimpl.updataShServiceImpl;

public class updataShController extends HttpServlet{
	updataShService upd = new updataShServiceImpl();
	
                 @Override
                protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                	 doPost(req, resp);
                }
                 
                 @Override
                protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                	//获取系统时间
                	 Date date=new Date();
                	 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                	 String drTime=format.format(date);
                     //修改库存
                	 String pIds=req.getParameter("pId");
               		int pId  = Integer.parseInt(pIds);
               	 String pAmounts=req.getParameter("pAmount");
            		int pAmount  = Integer.parseInt(pAmounts);
            		
            		
            		 //写入日志
               	 HttpSession session = req.getSession();
               	 Employee emp = (Employee) session.getAttribute("employee");
               	 String eName = emp.geteName();
               	 int eId = emp.geteId();
               	 int ssss = upd.getProuductCountByPid(pId).getpAmount();
               	 StringBuffer drInfos = new StringBuffer();
              		drInfos.append("用户");
              		drInfos.append(eName);
              		drInfos.append("把编号为");
              		drInfos.append(pId);
              		drInfos.append("的商品数量由");
              		drInfos.append(ssss);
              		drInfos.append("改为");
              		drInfos.append(pAmount);
              		String drInfo = drInfos.toString();
                    upd.setdrInfo(drInfo, drTime, eId, eName);

              		
            		//修改库存
                	 int s= upd.updataShByPid(pId, pAmount);
                	 String mm = Integer.toString(s);
                	 PrintWriter pw = resp.getWriter();
                	 pw.write(mm);
                	 
                	//写入订单及收支
                   	 int poAmount = pAmount;
                   	 double pCost = upd.getProuductCountByPid(pId).getpCost();
                   	 double oMoney = pAmount*pCost;
                   	 int oId = upd.getOidByPid(pId);
                        upd.updataOrder(oId, oMoney);
                        upd.updataOrderInfo(oId, poAmount);
                	 
                	
                	 
                	 
                }
}
